#include "ucollapsewidget.h"
#include "ulabel.h"

#include <QVBoxLayout>
#include <QMouseEvent>
#include <QFrame>
#include <QIcon>

class UCollapseWidgetPrivate
{
public:
    UCollapseWidgetPrivate(UCollapseWidget* q) : q_ptr(q) {}
    ~UCollapseWidgetPrivate() {}

    void init();

    void expandItem(bool isExpand);

private:
    Q_DECLARE_PUBLIC(UCollapseWidget)
    Q_DISABLE_COPY(UCollapseWidgetPrivate)
    UCollapseWidget* const q_ptr = nullptr;

    ULabel* titleLabel = nullptr;
    ULabel* iconLabel = nullptr;
    QWidget* contentWidget = nullptr;
    QVBoxLayout* layout = nullptr;
    QWidget* topWidget = nullptr;
    bool isExpanded = false;
};

void UCollapseWidgetPrivate::init()
{
    Q_Q(UCollapseWidget);
    titleLabel = new ULabel(q);
    titleLabel->setFontWeight(QFont::Bold);
    titleLabel->setAttribute(Qt::WA_TransparentForMouseEvents, true);
    iconLabel = new ULabel(q);
    iconLabel->setFixedSize(18, 18);
    iconLabel->setIcon(QIcon::fromTheme("utk_collapse"));
    iconLabel->setAttribute(Qt::WA_TransparentForMouseEvents, true);

    topWidget = new QWidget(q);
    topWidget->installEventFilter(q);
    topWidget->setMaximumHeight(22);
    QHBoxLayout* hLayout = new QHBoxLayout;
    hLayout->setContentsMargins(0, 0, 0, 0);
    hLayout->addWidget(titleLabel);
    hLayout->addStretch();
    hLayout->addWidget(iconLabel);
    topWidget->setLayout(hLayout);

    QFrame* line = new QFrame(q);
    line->setFrameShape(QFrame::HLine);

    contentWidget = new QWidget(q);
    contentWidget->setVisible(false);
    layout = new QVBoxLayout;
    layout->setContentsMargins(8, 0, 0, 0);
    contentWidget->setLayout(layout);

    QVBoxLayout* mainLayout = new QVBoxLayout;
    mainLayout->setContentsMargins(0, 0, 0, 0);
    mainLayout->addWidget(topWidget);
    mainLayout->addWidget(line);
    mainLayout->addWidget(contentWidget);
    q->setLayout(mainLayout);
}

void UCollapseWidgetPrivate::expandItem(bool isExpand)
{
    Q_Q(UCollapseWidget);
    isExpanded = isExpand;
    QIcon icon = isExpanded ? QIcon::fromTheme("utk_expand") : QIcon::fromTheme("utk_collapse");
    iconLabel->setIcon(icon);
    contentWidget->setVisible(isExpanded);
    emit q->expandChanged(isExpanded);
}

UCollapseWidget::UCollapseWidget(QWidget* parent) : UWidget(parent)
    , d_ptr(new UCollapseWidgetPrivate(this))
{
    Q_D(UCollapseWidget);
    d->init();
}

UCollapseWidget::~UCollapseWidget()
{
}

void UCollapseWidget::setTitleText(const QString &text)
{
    Q_D(UCollapseWidget);
    d->titleLabel->setText(text);
}

void UCollapseWidget::addItem(QWidget* widget)
{
    Q_D(UCollapseWidget);
    d->layout->addWidget(widget);
}

void UCollapseWidget::clearItem()
{
    Q_D(UCollapseWidget);
    QLayoutItem* child;
    while ((child = d->layout->takeAt(0)) != nullptr)
    {
        delete child->widget();
        delete child;
    }
}

void UCollapseWidget::setExpanded(bool isExpand)
{
    Q_D(UCollapseWidget);
    d->expandItem(isExpand);
}

bool UCollapseWidget::isExpanded()
{
    Q_D(UCollapseWidget);
    return d->isExpanded;
}

bool UCollapseWidget::eventFilter(QObject *watched, QEvent *event)
{
    Q_D(UCollapseWidget);
    if (watched == d->topWidget && event->type() == QEvent::MouseButtonRelease)
    {
        d->expandItem(!d->isExpanded);
        return true;
    }

    return UWidget::eventFilter(watched, event);
}
